import { apple } from '$lib/item';
import { addComponent, defineQuery } from 'bitecs';
import { generateMap } from '../../mapbuilder';
import { glassWindow, metalDoor } from '../door';
import { Drain, Drainage, Elevation, drain, drainageQuery, spring } from '../flooding';
import { tree } from '../foliage';
import { closet } from '../furniture';
import { Ground, rawGround, woodenFloor } from '../ground';
import { noteHello, noteILeftKeys } from '../notes';
import { brickWall, wall } from '../wall';
import { Clock } from '$lib/clock';
import { random } from '$lib/random';
import { landscapeQuery } from '$lib/landscape';
import { getPos2, landscapeAt } from '$lib/pos2';
import * as p5 from 'p5';

export const level = `
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z        *                                                     z
z                                                              z
z                                                              z
z                   0                                          z
z                                                              z
z                  8                                           z
z               %      #########:#:###                         z
z                      #...--#.......#  8                      z
z                      :.....+.......+                         z
z                88    #.....#.......#  8                      z
z                      #######.......:                         z
z                %     #...--#.......:                         z
z              888     :.....+.......:                         z
z               8      #.....#.......#                         z
z                 %    #######+#######                         z
z                      #.......]#                              z
z            1         :......--#                              z
z                      #......--#                              z
z                      ##########                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                  8 ###:##:####+###    %                      z
z                 %8 #.......--#..]#                           z
z                    :.......--#..]#   88                      z
z                    #.........#..]#    8                      z
z                 %  ########+##..]#                           z
z               88   #....--#......#                           z
z               8    :......#......#                           z
z                %   :......+......#    88  %                  z
z               %88  #....--#......#         8                 z
z               888% ########......#        888                z
z                    #.............#         8                 z
z               8    #.............#   88                      z
z                  % #.............# % 88                      z
z                88  #.............#                           z
z                 8  ###############                           z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
z                                                              z
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
`;

export const prepareMap = (world) => {
  generateMap(world, level, rawGround, {
    '.': woodenFloor,
    '#': brickWall,
    '+': metalDoor,
    ':': glassWindow,
    '8': tree,
    ']': closet,
    // '%': apple,
    '0': noteILeftKeys,
    '1': noteHello,
    '*': spring,
    ';': drain,
    'z': wall,
  });
  let noise;
  new p5((p) => {
    p.noiseSeed(1234);
    noise = p.noise;
    // UGH this doesn't work for some reason
    p.noCanvas();
  });
  for (let e of landscapeQuery(world)) {
    addComponent(world, Elevation, e);
    let {x, y} = getPos2(e);
    Elevation.level[e] = Math.floor(noise(x*0.2, y*0.2)*30);
  }
  for (let e of drainageQuery(world)) {
    const {x, y} = getPos2(e);
    const land = landscapeAt(world, x, y);
    addComponent(world, Drain, land);
    Drain.amount[land] = Drainage.amount[e];
  }
};
